//
//  AiAdvertiseHelper.m
//  ROSE
//
//  Created by 艾阳 on 2018/5/23.
//  Copyright © 2018年 HeNanZhongDunYunAn. All rights reserved.
//

#import "AiAdvertiseHelper.h"

static NSString *const adImageName = @"adImageName";

@implementation AiAdvertiseHelper

static AiAdvertiseHelper* _instance = nil;
+ (instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    return _instance;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(adClick:) name:NotificationConstants_Advertise_Key object:nil];
    }
    return self;
}
/*
 展示广告页面
 */
+(void)showAdvertiserView:(NSArray<NSString *> *)imageArray{
    //1.判断沙盒中是否存在广告图片 如果存在，直接显示
    NSString *filePath = [[AiAdvertiseHelper sharedInstance] getFilePathWithImageName:[NSUserDefaults.standardUserDefaults valueForKey:adImageName]];
    
    BOOL isExist = [[AiAdvertiseHelper sharedInstance] isFileExistWithFilePath:filePath];
    
    if (isExist) {
        
    }
    
}


/*
 根据图片名拼接文件路径
 */
- (NSString *)getFilePathWithImageName:(NSString *)imageName{
    if (imageName) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:imageName];
        return filePath;
    }
    return nil;
}

/*
 判断文件是否存在
 */
- (BOOL)isFileExistWithFilePath:(NSString *)filePath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDirectory = FALSE;
    return [fileManager fileExistsAtPath:filePath isDirectory:&isDirectory];
}

/*
 点击广告页面
 */
-(void)adClick:(NSNotification *)noti{
    NSString *url = @"https://github.com";
    if (!IsEmpty(url)) {
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]]) {
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:url] options:@{} completionHandler:^(BOOL success) {}];
        }
    }
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
